<script>
export default {
  name: 'CartTools',
  localStorageKey: 'YangfanShop_CartCache',
  setAllList:function (allList) {
    var _allList=[]
    allList.forEach(function (item,index) {
      _allList.push({
        shop:{
          id:item.shop.id,
          name:item.shop.name,
          logo:item.shop.logo,
        },
        productList:item.productList,
      })
    })
    localStorage.setItem(this.localStorageKey,JSON.stringify(_allList))
  },
  getAllList:function () {
    var allProductList = []
    var cacheString = localStorage.getItem(this.localStorageKey)
    if (cacheString) {
      allProductList = JSON.parse(cacheString)
    }
    return allProductList;
  },
  // 商品-增加
  addProduct: function (productParams) {
    var allProductList =this.getAllList()
    var currShopIndex = -1
    var currProductIndex = -1
    allProductList.forEach(function (item, index) {
      if (productParams.shopId === item.shop.id) {
        currShopIndex = index
        item.productList.forEach(function (_item, _index) {
          if (_item.id === productParams.id && _item.model===productParams.model) {
            currProductIndex = _index
          }
        })
      }
    })

    // 如果店铺是空
    if (currShopIndex === -1) {
      allProductList.push({
        shop: {
          id: productParams.shopId,
          logo: productParams.shopLogo,
          name: productParams.shopName,
        },
        productList: [
          {
            id: productParams.id,
            image: productParams.image,
            name: productParams.name,
            price: productParams.price,
            model: productParams.model,
            nums: productParams.nums
          }
        ]
      })
    } else {
      if (currProductIndex === -1) {
        allProductList[currShopIndex].productList.push({
          id: productParams.id,
          image: productParams.image,
          name: productParams.name,
          price: productParams.price,
          model: productParams.model,
          nums: productParams.nums
        })
      } else {
        allProductList[currShopIndex].productList[currProductIndex].nums++
      }
    }
    localStorage.setItem(this.localStorageKey, JSON.stringify(allProductList))
  },
  // 商品-减少
  subProduct: function (productId) {
    var allProductList =this.getAllList()
    var currShopIndex = -1
    var currProductIndex = -1
    allProductList.forEach(function (item, index) {
      if (productParams.shopId === item.shop.id) {
        currShopIndex = index
        item.productList.forEach(function (_item, _index) {
          if (_item.id === productParams.id && _item.model===productParams.model) {
            currProductIndex = _index
          }
        })
      }
    })

    // 如果店铺是空
    if (currShopIndex !== -1 && currProductIndex !== -1) {
      allProductList[currShopIndex].productList[currProductIndex].nums--
    }
    var realProductList = []
    allProductList.forEach(function (item, index) {
      var tmpList = []
      item.productList.forEach(function (_value, _index) {
        if (_value.nums !== 0) {
          tmpList.push(_value)
        }
      })
      // 如果有商品列表,才会压入正式数组
      if (tmpList.length > 0) {
        realProductList.push({
          shop: item.shop,
          productList: tmpList,
        })
      }
    })
    localStorage.setItem(this.localStorageKey, JSON.stringify(realProductList))
  },
  // 删除-商品
  delProduct: function (productId,model) {
    var allProductList =this.getAllList()
    var realProductList = []
    allProductList.forEach(function (item, index) {
      var tmpList = []
      item.productList.forEach(function (_item, _index) {
        if (!(_item.id === productId && _item.model===model)) {
          tmpList.push(_item)
        }
      })
      if (tmpList.length > 0) {
        realProductList.push({
          shop: item.shop,
          productList: tmpList,
        })
      }
    })
    localStorage.setItem(this.localStorageKey, JSON.stringify(realProductList))
  },
  // 删除-店铺
  delShop: function (shopId) {
    var allProductList =this.getAllList()
    var tmpAllList=[]
    allProductList.forEach(function (item, index) {
      if (item.shop.id!==shopId){
        tmpAllList.push(item)
      }
    })
    localStorage.setItem(this.localStorageKey, JSON.stringify(tmpAllList))
  },
  // 删除-商品
  deleteItem: function (productId, productList) {
    var arr = []
    productList.forEach(function (value, index) {
      if (productId !== value.id) {
        arr.push(value)
      }
    })
    return arr
  }
}
</script>
